package com.pingxx.boot.demo.validation;

import org.springframework.util.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PhoneValidator implements ConstraintValidator<Phone, CharSequence> {

    private static final Pattern mobile_pattern = Pattern.compile("1\\d{10}");

    private Phone constraintAnnotation;

    @Override
    public boolean isValid(CharSequence value, ConstraintValidatorContext context) {
        if (!StringUtils.isEmpty(value)) {
            Matcher m = mobile_pattern.matcher(value);
            return m.matches();
        }
        return false;
    }

    @Override
    public void initialize(Phone constraintAnnotation) {
        this.constraintAnnotation = constraintAnnotation;
    }
}
